package com.southwind.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.anno.Log;
import com.southwind.common.DataGridView;
import com.southwind.common.PageModel;
import com.southwind.common.ResultObj;
import com.southwind.entity.AdInfo;
import com.southwind.entity.AdPlan;
import com.southwind.entity.SysUser;
import com.southwind.mapper.SysUserMapper;
import com.southwind.service.AdService;
import com.southwind.service.AdPlanService;
import com.southwind.util.WebUtils;
import com.southwind.vo.AdPlanVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/convertApply")
public class AdPlanController {

    @Autowired
    private AdPlanService convertApplyService;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private AdService adInfoService;

    @RequestMapping("/loadAllconvertApply")
    public DataGridView loadAllconvertApply(PageModel pageModel){
        Page<AdPlan> page = new Page<>(pageModel.getPage(), pageModel.getLimit());
        Page<AdPlan> resultPage = this.convertApplyService.page(page);
        List<AdPlanVO> list = new ArrayList<>();
        for (AdPlan record : resultPage.getRecords()) {
            AdPlanVO vo = new AdPlanVO();
            BeanUtils.copyProperties(record, vo);
            vo.setApplyName(this.userMapper.getUserNameById(record.getApplyUserId()));
            vo.setApprovalName(this.userMapper.getUserNameById(record.getApprovalUserId()));
            list.add(vo);
        }
        return new DataGridView(resultPage.getTotal(), list);
    }
    // 获取所有广告名称列表
    @GetMapping("/getAdNames")
    public List<AdInfo> getAdNames() {
        return adInfoService.list();
    }
    @Log
    @RequestMapping("/addConvertApply")
    public ResultObj addConvertApply(AdPlan convertApply){
        convertApply.setStatus(0);
        SysUser user = (SysUser) WebUtils.getSession().getAttribute("user");
        convertApply.setApplyUserId(user.getId());
        boolean save = this.convertApplyService.save(convertApply);
        if(save) return ResultObj.ADD_SUCCESS;
        return ResultObj.ADD_ERROR;
    }
    @Log
    @RequestMapping("/updateConvertApply")
public ResultObj updateConvertApply(AdPlan convertApply) {
    convertApply.setApprovalDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    SysUser user = (SysUser) WebUtils.getSession().getAttribute("user");

    // 获取当前用户的 loginname
    String loginName = user.getLoginname();

    // 如果不是 system 用户，则执行不能审核自己的逻辑
    if (!"system".equals(loginName) && user.getId().equals(convertApply.getApplyUserId())) {
        return ResultObj.APPROVAL_ALREADY_ERROR;
    }

    convertApply.setApprovalUserId(user.getId());
    boolean updateById = this.convertApplyService.updateById(convertApply);
    if (updateById) return ResultObj.APPROVAL_SUCCESS;
    return ResultObj.APPROVAL_ERROR;
}
@RequestMapping("/revoke")
public ResultObj revoke(Integer id) {
    AdPlan apply = new AdPlan();
    apply.setId(id);
    apply.setStatus(3); // 紧急撤销状态
    boolean updated = this.convertApplyService.updateById(apply);
    if (updated) {
        return ResultObj.REVOKE_SUCCESS;
    }
    return ResultObj.REVOKE_ERROR;
}



}